#pragma once
#include <iostream>
#include <string>
#include<unistd.h>
#include<sys/types.h>
#include <jsoncpp/json/json.h>
namespace protocol_ns
{
    const std::string SEP = "\r\n";
    //json串变成"len"\r\n"{ ...... }"\r\n添加长度报头啥的
    std::string Encode(const std::string &json_str)
    {
        int json_str_len = json_str.size();
        std::string proto_str = std::to_string(json_str_len);
        proto_str += SEP;
        proto_str += json_str;
        proto_str += SEP;
        return proto_str;
    }
    //看inbuffer中有没有完整的请求
    std::string Decode(std::string &inbuffer)
    {
        auto pos = inbuffer.find(SEP);
        //找不到说明报文一定不完整
        if(pos == std::string::npos)//返回空串
            return std::string();
        //(0~/r之前)
        std::string len_str = inbuffer.substr(0,pos);
        if(len_str.empty())
            return std::string();
        int pagelen = std::stoi(len_str);
        //总长度=json串的长度+json串长度的长度+\r\n的长度
        int total = pagelen + len_str.size() + 2 * SEP.size();
        //如果inbuffer的长度>=total说明一定有一个完整请求
        if(inbuffer.size() < total)   
            return std::string();

        //返回json串  len\r\n{         }\r\n 从\n后面度串的长度
        std::string package = inbuffer.substr(pos + SEP.size(),pagelen);
        inbuffer.erase(total);
        return package;
    }
class Request
{
public:
    Request()
    {}
    Request(int x,int y,char oper):_x(x),_y(y),_oper(oper)
    {}
    bool Serialize(std:: string *out)
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;
        
        Json::FastWriter writer;
        *out = writer.write(root);
        return true;
    }
    bool DeSerialize(const std::string &in)
    {
        Json::Value root;
        Json::Reader reader;
        bool res = reader.parse(in,root);
        if(!res)
            return false;
        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();
        return res;
    }
    
    ~Request()
    {}
public:
    int _x;
    int _y;
    char _oper;

};
class Response
{
public:
    Response()
    {
    }
    Response(int result,int code):_result(result),_code(code)
    {
    }
    bool Serialize(std:: string *out)
    {
        Json::Value root;
        root["code"] = _code;
        root["result"] = _result;

        Json::FastWriter writer;
        *out = writer.write(root);
        return true;
    }
    bool DeSerialize(const std::string &in)
    {
        Json::Value root;
        Json::Reader reader;
        bool res = reader.parse(in,root);
        if(!res)
            return false;
        _code = root["code"].asInt();
        _result = root["result"].asInt();
        return res;
    }
    ~Response()
    {

    }
public:
    int _result;
    int _code;
};
class Factory
{
public:
    Factory()
    {
        srand(time(nullptr) ^ getpid());
        opers = "+-*/%";
    }
    //通过随机数来获取数字和运算符
    std::shared_ptr<Request> BulidRequest()
    {
        int x = rand()%10+1;
        usleep(20*x);
        int y = rand()%5;
        usleep(10*y);
        char oper = opers[rand()%opers.size()];
        std::shared_ptr<Request> req = std::make_shared<Request>(x,y,oper);
        return req;
    }
    std::shared_ptr<Response> BulidResponse()
    {
        return std::make_shared<Response>();
    }
    ~Factory()
    {}
private:
    std::string opers;
};
}
